package com.example.temporalencrypter;

import java.io.File;
import java.util.Calendar;

import com.ipaulpro.afilechooser.utils.FileUtils;

import encrypter.Encrypter;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

public class DecryptActivity extends Activity {
	private static final int REQUEST_CHOOSER = 1234;
	File fileSelected;
	private Calendar cal;
	private int day;
	private int month;
	private int year;
	private int hour;
	private int min;
	private String fullDate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_decrypt);
		cal = Calendar.getInstance();
		

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case REQUEST_CHOOSER:
			if (resultCode == RESULT_OK) {

				final Uri uri = data.getData();

				String path = FileUtils.getPath(this, uri);

				fileSelected = FileUtils.getFile(this, uri);
				if (path != null && FileUtils.isLocal(path)) {
					fileSelected = new File(path);
				}

			}

			break;
		}
		TextView txtView1 = (TextView) findViewById(R.id.selectedFileTextDecrypt);
		if (fileSelected != null) {
			txtView1.setText(fileSelected.getPath());
		}

	}

	@Override
	protected void onStart() {
		super.onStart();
		Button btChooseFile = (Button) findViewById(R.id.chooseFileButtonDecrypt);
		Button btDoDecrypt = (Button) findViewById(R.id.doDecryptButton);
		btChooseFile.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				
				Toast.makeText(getApplicationContext(),
				
						"Seleccione el archivo", Toast.LENGTH_SHORT).show();

				Intent getContentIntent = FileUtils.createGetContentIntent();

				Intent intent = Intent.createChooser(getContentIntent,
						"Select a file");
				startActivityForResult(intent, REQUEST_CHOOSER);

			}
		});

		btDoDecrypt.setOnClickListener(new View.OnClickListener() {

			public void onClick(View arg0) {
				if(fileSelected!= null){
					File newFile = new File(fileSelected.getParentFile()
							+ "/decripted");
					if(Encrypter.get().canDecrypt(fileSelected, Calendar.getInstance())){
						Encrypter.get().decrypt(fileSelected);
						Toast.makeText(getApplicationContext(),
								"Archivo Desencriptado!", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(getApplicationContext(),
								"Aun no se puede desencriptar el archivo!", Toast.LENGTH_SHORT).show();
					}
					
					
				}else{
					Toast.makeText(getApplicationContext(),
							"Seleccione un archivo antes", Toast.LENGTH_SHORT).show();
				}

				

			}
		});

	}



}